home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / lib / tk / demos / mkPlot.tcl < prev    next >
Encoding:
Text File  |  1995-07-21  |  2.3 KB  |  76 lines

  1. # mkPlot w
  2. #
  3. # Create a top-level window containing a canvas displaying a simple
  4. # graph with data points that can be moved interactively.
  5. #
  6. # Arguments:
  7. #    w -    Name to use for new top-level window.
  8.  
  9. proc mkPlot {{w .plot}} {
  10.     catch {destroy $w}
  11.     toplevel $w
  12.     dpos $w
  13.     wm title $w "Plot Demonstration"
  14.     wm iconname $w "Plot"
  15.     set c $w.c
  16.  
  17.     message $w.msg -font -Adobe-Times-Medium-R-Normal-*-180-* -width 400 \
  18.         -bd 2 -relief raised -text "This window displays a canvas widget containing a simple 2-dimensional plot.  You can doctor the data by dragging any of the points with mouse button 1."
  19.     canvas $c -relief raised -width 450 -height 300
  20.     button $w.ok -text "OK" -command "destroy $w"
  21.     pack $w.msg $w.c -side top -fill x
  22.     pack $w.ok -side bottom -pady 5
  23.  
  24.     set font -Adobe-helvetica-medium-r-*-180-*
  25.  
  26.     $c create line 100 250 400 250 -width 2
  27.     $c create line 100 250 100 50 -width 2
  28.     $c create text 225 20 -text "A Simple Plot" -font $font -fill brown
  29.     
  30.     for {set i 0} {$i <= 10} {incr i} {
  31.     set x [expr {100 + ($i*30)}]
  32.     $c create line $x 250 $x 245 -width 2
  33.     $c create text $x 254 -text [expr 10*$i] -anchor n -font $font
  34.     }
  35.     for {set i 0} {$i <= 5} {incr i} {
  36.     set y [expr {250 - ($i*40)}]
  37.     $c create line 100 $y 105 $y -width 2
  38.     $c create text 96 $y -text [expr $i*50].0 -anchor e -font $font
  39.     }
  40.     
  41.     foreach point {{12 56} {20 94} {33 98} {32 120} {61 180}
  42.         {75 160} {98 223}} {
  43.     set x [expr {100 + (3*[lindex $point 0])}]
  44.     set y [expr {250 - (4*[lindex $point 1])/5}]
  45.     set item [$c create oval [expr $x-6] [expr $y-6] \
  46.         [expr $x+6] [expr $y+6] -width 1 -outline black \
  47.         -fill SkyBlue2]
  48.     $c addtag point withtag $item
  49.     }
  50.  
  51.     $c bind point <Any-Enter> "$c itemconfig current -fill red"
  52.     $c bind point <Any-Leave> "$c itemconfig current -fill SkyBlue2"
  53.     $c bind point <1> "plotDown $c %x %y"
  54.     $c bind point <ButtonRelease-1> "$c dtag selected"
  55.     bind $c <B1-Motion> "plotMove $c %x %y"
  56. }
  57.  
  58. set plot(lastX) 0
  59. set plot(lastY) 0
  60.  
  61. proc plotDown {w x y} {
  62.     global plot
  63.     $w dtag selected
  64.     $w addtag selected withtag current
  65.     $w raise current
  66.     set plot(lastX) $x
  67.     set plot(lastY) $y
  68. }
  69.  
  70. proc plotMove {w x y} {
  71.     global plot
  72.     $w move selected [expr $x-$plot(lastX)] [expr $y-$plot(lastY)]
  73.     set plot(lastX) $x
  74.     set plot(lastY) $y
  75. }
  76.